package com.ios.controller;

import com.ios.service.ViolationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ios.model.entity.Violation;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import java.util.List;


@RestController
@RequestMapping("/violation")
public class ViolationController extends BaseController {

    private final ViolationService violationService;

    @Autowired
    public ViolationController(ViolationService violationService) {
        this.violationService = violationService;
    }

    @GetMapping("/importUncheckedToViolation")
    public AjaxResult importUncheckedToViolation() {
        violationService.importUncheckedToViolation(5);
        return AjaxResult.success("更改数据成功");
    }

    /**
     * id获取用户的违规记录
     */
    @GetMapping("/user/{id}")
    public TableDataInfo getViolationByUserId(@PathVariable("id") Long id) {
        startPage();
        return getDataTable(violationService.getViolationByUserId(id));
    }

    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody Violation violation) {
        violationService.updateStatus(violation.getId(),violation.getUserId(),violation.getAppealStatus());
        return AjaxResult.success("更新成功");
    }

    @PutMapping("/reject")
    public AjaxResult rejectUpdateStatus(@RequestBody Violation violation) {
        violationService.rejectUpdateStatus(violation.getId(),violation.getUserId());
        return AjaxResult.success("更新成功");
    }

    /**
     * 查询违规处理列表
     */
    @PreAuthorize("@ss.hasPermi('system:violation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Violation violation)
    {
        startPage();
        List<Violation> list = violationService.selectViolationList(violation);
        return getDataTable(list);
    }

    /**
     * 导出违规处理列表
     */
    @PreAuthorize("@ss.hasPermi('system:violation:export')")
    @Log(title = "违规处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Violation violation)
    {
        violationService.importUncheckedToViolation(5);
        List<Violation> list = violationService.selectViolationList(violation);
        ExcelUtil<Violation> util = new ExcelUtil<Violation>(Violation.class);
        util.exportExcel(response, list, "违规处理数据");
    }

    /**
     * 获取违规处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:violation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(violationService.selectViolationById(id));
    }

    /**
     * 新增违规处理
     */
    @PreAuthorize("@ss.hasPermi('system:violation:add')")
    @Log(title = "违规处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Violation violation)
    {
        return toAjax(violationService.insertViolation(violation));
    }

    /**
     * 修改违规处理
     */
    @Log(title = "违规处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Violation violation)
    {
        return toAjax(violationService.updateViolation(violation));
    }

    /**
     * 删除违规处理
     */
    @PreAuthorize("@ss.hasPermi('system:violation:remove')")
    @Log(title = "违规处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(violationService.deleteViolationByIds(ids));
    }


}
